#include <gfx/mesh.h>
#include <gfx/iindexbuffer.h>
#include <gfx/ivertexbuffer.h>
#include <gfx/ivertexdeclaration.h>

ceMesh::ceMesh()
	: CE_ASSET_SUPER()
	, _vertices(0)
  , _indices(0)
  , _vertexDeclaration(0)
  , _primCount(0)
  , _indexDataType(DT_UnsignedShort)
  , _primType(PT_Triangles)
{
  CE_OBJECT_CONSTR
}

ceMesh::~ceMesh()
{
  CE_UNSET(_vertices);
  CE_UNSET(_indices);
  CE_UNSET(_vertexDeclaration);
}

void ceMesh::SetVertices(iVertexBuffer *vertices)
{
  CE_SET(_vertices, vertices);
}

iVertexBuffer* ceMesh::GetVertices()
{
  return _vertices;
}

const iVertexBuffer* ceMesh::GetVertices() const
{
  return _vertices;
}

void ceMesh::SetIndices(iIndexBuffer *indices)
{
  CE_SET(_indices, indices);
}

iIndexBuffer* ceMesh::GetIndices()
{
  return _indices;
}

const iIndexBuffer* ceMesh::GetIndices() const
{
  return _indices;
}

void ceMesh::SetVertexDeclaration(iVertexDeclaration *vertexDeclaration)
{
  CE_SET(_vertexDeclaration, vertexDeclaration);
}

iVertexDeclaration* ceMesh::GetVertexDeclration()
{
  return _vertexDeclaration;
}

void ceMesh::SetBoundingBox(const ceBoundingBox &bbox)
{
  _bbox = bbox;
}

ceBoundingBox& ceMesh::GetBoundingBox()
{
  return _bbox;
}

const ceBoundingBox& ceMesh::GetBoundingBox() const
{
  return _bbox;
}

void ceMesh::SetPrimitiveType(cePrimitiveType type)
{
  _primType = type;
}

cePrimitiveType ceMesh::GetPrimitiveType() const
{
  return _primType;
}

void ceMesh::SetNumberOfTrigons(unsigned numberOfTrigons)
{
  _primCount = numberOfTrigons * 3;
}

unsigned ceMesh::GetNumberOfTrigons() const
{
  return _primCount / 3;
}

void ceMesh::SetNumberOfLines(unsigned numberOfLines)
{
  _primCount = numberOfLines * 2;
}

unsigned ceMesh::GetNumberOfLines() const
{
  return _primCount / 2;
}

unsigned ceMesh::GetNumberOfPrims() const
{
  return _primCount;
}

void ceMesh::Set32BitIndices(bool bit32)
{
  _indexDataType = bit32 ? DT_UnsignedInt : DT_UnsignedShort;
}

bool ceMesh::Is32BitIndices() const
{
  return _indexDataType == DT_UnsignedInt;
}

ceDataType ceMesh::GetIndexDataType() const
{
  return _indexDataType;
}


